
AccordionMenu = function(options) {
	this.config = {
			containerCls : '#menu_list', // 外层容器
			menuArrs : '', //  JSON传进来的数据
			type : 'click', // 默认为click 也可以mouseover
			renderCallBack : null, // 渲染html结构后回调
			clickItemCallBack : null,
			bootUrl:bootUrl
		// 每点击某一项时候回调
		};
		this.cache = {

		};
		this.init(options);
	}
 AccordionMenu.prototype = {
		 constructor : AccordionMenu,
			init : function(options) {
				this.config = $.extend(this.config, options || {});
				var self = this, _config = self.config, _cache = self.cache;
				// 渲染html结构
				$(_config.containerCls).each(function(index, item) {
					self._renderHTML(item);
				});
			},
			_renderHTML : function(container) {
				var self = this, _config = self.config, _cache = self.cache;
				$(_config.menuArrs).each(function(index, item) {
									var lihtml ;
									if(item.url!=null && item.url!=''){
										if(item.mainPic==null||undefined||''){
											var subLi = $('<ul><li><a style="background:url('+_config.bootUrl+'/images/admin/mainAdd.png) no-repeat 28px 10px" _href="' + item.url 
													+ '" href="javascript:void(0)">' + item.name
													+ '</a></li></ul>');
										}else{
											var subLi = $('<ul><li><a style="background:url('+_config.bootUrl+'/images/admin/'+item.code+'.png) no-repeat 28px 10px" _href="' + item.url 
													+ '" href="javascript:void(0)">' + item.name
													+ '</a></li></ul>');
										}
										
										$(container).append(subLi);
									}else{
										var ulhtml = $('<dl id="'+item.code+'"></dl>');
										if(index==0){
											if(item.mainPic==null||undefined||''){
												lihtml = $('<dt class="selected">'
														+ '<span style="background:url('+_config.bootUrl+'/images/admin/mainAdd.png) no-repeat 20px 0px">'+item.name+'</span>'
														+ '<i class="iconfont menu_dropdown-arrow">&#xf02af;</i></dt>');
											}else{
												lihtml = $('<dt class="selected">'
														+ '<span style="background:url('+_config.bootUrl+'/images/admin/'+item.code+'.png) no-repeat 20px 0px">'+item.name+'</span>'
														+ '<i class="iconfont menu_dropdown-arrow">&#xf02af;</i></dt>');
											}
											
										}else{
											if(item.mainPic==null||undefined||''){
												lihtml = $('<dt>'
														   + '<span style="background:url('+_config.bootUrl+'/images/admin/mainAdd.png) no-repeat 20px 0px">'+item.name+'</span>'
														   + '<i class="iconfont menu_dropdown-arrow">&#xf02af;</i></dt>');
											}else{
												lihtml = $('<dt>'
														   + '<span style="background:url('+_config.bootUrl+'/images/admin/'+item.code+'.png) no-repeat 20px 0px">'+item.name+'</span>'
														   + '<i class="iconfont menu_dropdown-arrow">&#xf02af;</i></dt>');
											}
										   
										}
										$(ulhtml).append(lihtml);
										if (item.submenu && item.submenu.length > 0) {
											self._createSubMenu(item.submenu, ulhtml,index);
										}
										$(container).append(ulhtml);
									}
									
								});
				_config.renderCallBack && $.isFunction(_config.renderCallBack)
						&& _config.renderCallBack();
			},
			/**
			 * 创建子菜单
			 * @param {array} 子菜单
			 * @param {lihtml} li项
			 */
			_createSubMenu : function(submenu, lihtml,index) {
				var self = this, _config = self.config, _cache = self.cache, _subUl = $('<ul></ul>');
				var _subDD = index==0 ?$('<dd style="display:block;"></dd>'):$('<dd></dd>')
				 
				$(_subDD).append(_subUl);
				$(submenu).each(
						function(index, item) {
							var url = item.url || 'javascript:void(0)';
							var subLi = $('<li><a _href="' + url 
									+ '" href="javascript:void(0)">' + item.name
									+ '</a></li>');
							$(_subUl).append(subLi);
						});
				$(lihtml).append(_subDD);
		 }
	};
 
   loadMenu = function(ajaxSource) {
			$.ajax({
				type : "GET",
				url : ajaxSource,
				dataType : "JSON",
				async :false,
				contentType : "application/json",
				success : this._callback
			});
		}
	 
	_callback = function(dataObj) {
			if (dataObj != null && dataObj.flag == true) {
				 new AccordionMenu({
					menuArrs : dataObj.results
				 });
			 }
		}
	loadMenu('resource/loadMenu?d='+new Date().getTime())
$(function(){
	if($('.main_touxAdress').text()=="null"){
		$('.main_touxAdress').text("");
	}
	$('#main_quit').click(function(){
		layer.confirm('您确定退出系统吗?',function(){
			$.ajax({
				type : "post",
				url : bootUrl+'/api/session/loginout',
				dataType : "json",
				contentType : "application/json",
				data : JSON.stringify({}),
				success : function(result) {
					if (result.flag) {
						location.reload();
					}
				}
			});	
		})
		
	})

	$('#main_setPass').click(function(){
		layer_show(650,'400','修改密码',bootUrl+'/api/session/changepassword');		
	})
	
	$('#back_mainPage').click(function(){
		window.location = bootUrl;		
	})
	
	var iframeBox=$("#iframe_box").find("iframe:first");
	$('#menu_list>dl').attr('name','dl');
	$('#menu_list>ul').attr('name','ul');
	var menuListNav=$("#menu_list").find("a:first");
	if(menuListNav.parent().parent().attr('name')=='ul'){
		$("#Hui-tabNav").html("<i class='iconfont'>&#xf012b;</i> <span id='nav-list'>"+menuListNav.text());
	}else{
		$("#Hui-tabNav").html("<i class='iconfont'>&#xf012b;</i><span id='nav-list'>"+menuListNav.parents('dl').find('span').text()+"<span class='c-gray en'>></span>"+menuListNav.text()+"</span>");
		//$("#Hui-tabNav").html("<i class='iconfont'>&#xf012b;</i> 首页 <span id='nav-list'><span class='c-gray en'>></span>"+menuListNav.parents('dl').find('span').text()+"<span class='c-gray en'>></span>"+menuListNav.text()+"</span>");
	}
	
	if(typeof(menuListNav)!="{}"){
		iframeBox.attr('src',bootUrl+"/api/"+menuListNav.attr('_href')); 
	}else{
		iframeBox.attr('src','');
	}
	$('#menu_list dl a').on('click',function(e){
		e.stopPropagation();
		window.localStorage.clear();
		$(e.currentTarget).css({'paddingLeft':'55px','borderLeft':'8px solid #8bc34a'});
		$(e.currentTarget).parent().siblings().find('a').css({'paddingLeft':'63px','borderLeft':'none'})
		$(e.currentTarget).parents('dl').siblings().find('a').css({'paddingLeft':'63px','borderLeft':'none'});
	})
	$('#menu_list>dl,#menu_list>ul').on('click',function(e){
		e.stopPropagation();
		window.localStorage.clear();
		if($(e.currentTarget).attr('name')=='ul'){
			$(e.currentTarget).find('li').css('background','#8bc34a');
			$(e.currentTarget).siblings().find('li').css('background','#26344b');
			$('#menu_list>dl').find('dt').css('background','#26344b');
			$('#menu_list>dl').find("a").attr('style',"'paddingLeft':'63px';'borderLeft':'none'");
		}else if($(e.currentTarget).attr('name')=='dl'){
			$(e.currentTarget).find('dt').css('background','#8bc34a');
			$(e.currentTarget).siblings().find('dt').css('background','#26344b');
			$('#menu_list>ul').find('li').css('background','#26344b')
		}
	})
})